// This script requires three user-made packages: ppmlhdfe, did_imputation and coefplot.

cd "./unbundling-replication"

use "data/icc.dta", clear

xtset cid year

gen trade_ln = log(trade_openness)
gen gdp_ln = log(gdp)
gen gdppc_ln = log(gdppc)
gen fdi_stock_ln = log(fdi_stock)

// IHS Transformations for BJS analyses
gen total_ihs = asinh(total)
gen complainant_ihs = asinh(complainant)
gen defendant_ihs = asinh(defendant)

gen total_seat_ihs = asinh(total_seat)
gen bycourt_ihs = asinh(bycourt)
gen byparty_ihs = asinh(byparty)

replace ml_year = . if ml_year == 0

// Label for regression output
label variable ml "Model Law" 
label variable ml_treat "Model Law" 
label variable trade_ln "ln Trade Openness" 
label variable gdp_ln "ln GDP"
label variable growth "Growth"
label variable gdppc_ln "ln GDP per cap."
label variable v2x_rule "Rule of Law"
label variable nyc "NYC"
label variable fdi_stock_ln "ln FDI stock"

label variable total_seat_ihs "Total"
label variable total_ihs "Total"
label variable bycourt_ihs "Court"
label variable byparty_ihs "Parties"

glo econ trade_ln fdi_stock_ln gdp_ln gdppc_ln growth
glo inst nyc v2x_rule

/*
	Table 3, Panel A: Seat of ICC Arbitration
*/

/* PPML Analyses */

ppmlhdfe total_seat i.ml, absorb(cid year) cluster(cid)
est store icc_seat_base
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1992"

// Estimate pretrends p-value
ppmlhdfe total_seat i.ml F(3/1)i.ml_treat, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_seat_base
estadd local pretrents_p $temp_p
macro drop temp_p


ppmlhdfe total_seat i.ml $econ, absorb(cid year) cluster(cid)
est store icc_seat_cntrls1
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1992"

// Estimate pretrends p-value
ppmlhdfe total_seat i.ml F(3/1)i.ml_treat $econ, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_seat_cntrls1
estadd local pretrents_p $temp_p
macro drop temp_p

ppmlhdfe total_seat i.ml $econ $inst, absorb(cid year) cluster(cid) d
est store icc_seat_cntrls2
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1992"

// Estimate pretrends p-value
ppmlhdfe total_seat i.ml F(3/1)i.ml_treat $econ $inst, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_seat_cntrls2
estadd local pretrents_p $temp_p
macro drop temp_p

ppmlhdfe bycourt i.ml $econ $inst, absorb(cid year) cluster(cid)
est store icc_court_cntrl2
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1994"

// Estimate pretrends p-value
ppmlhdfe bycourt i.ml F(5/1)i.ml_treat $econ $inst, absorb(cid year) cluster(cid)
test 1F5.ml_treat 1F4.ml_treat 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_court_cntrl2
estadd local pretrents_p $temp_p
macro drop temp_p

ppmlhdfe byparty i.ml $econ $inst, absorb(cid year) cluster(cid)
est store icc_party_cntrl2
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1994"

// Estimate pretrends p-value
ppmlhdfe byparty i.ml F(3/1)i.ml_treat $econ $inst, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_party_cntrl2
estadd local pretrents_p $temp_p
macro drop temp_p


// Output results for Table 3, Panel A (PPML) and Table A14

esttab  icc_seat_base icc_seat_cntrls1 icc_seat_cntrls2 icc_court_cntrl2 icc_party_cntrl2 ///
	using "output/tables/table-A14.tex", /// 
	label s(start_year fe_year fe_unit pretrents_p N, ///
	label("Start Year" "Year FE?" "Country FE?" "Pre-trends p-value" "Observations") fmt(%9.0gc)) /// 
	mgroups("Total" "Court" "Parties", pattern(1 0 0 1 1) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) ///
			span erepeat(\cmidrule(lr){@span})) ///
	star(* .1 ** .05 *** .01) b(3) se(3) booktabs compress ///
	drop(0.* _cons) noomitted ///
	nomtitles nogap replace

/* BJS Analyses */

qui eststo seat_total_nc: did_imputation total_seat_ihs cid year ml_year, /// 
	cluster(cid)  ///
	autosample pre(3) ///
	tol(1) fe(cid year)
estadd local cntrl_econ ""
estadd local cntrl_pol ""

qui eststo seat_total_econ: did_imputation total_seat_ihs cid year ml_year, /// 
	cluster(cid) controls($econ) ///
	autosample pre(3) ///
	tol(1) fe(cid year)
estadd local cntrl_econ "\cmark"
estadd local cntrl_pol ""
		
qui eststo seat_total_full: did_imputation total_seat_ihs cid year ml_year, /// 
	cluster(cid) controls($econ $inst) ///
	autosample pre(3) ///
	tol(1) fe(cid year nyc)
estadd local cntrl_econ "\cmark"
estadd local cntrl_pol "\cmark"
		
qui eststo seat_court: did_imputation bycourt_ihs cid year ml_year, /// 
	cluster(cid) controls($econ $inst) ///
	autosample pre(3) ///
	tol(1) fe(cid year nyc) 
estadd local cntrl_econ "\cmark"
estadd local cntrl_pol "\cmark"
	
qui eststo seat_party: did_imputation byparty_ihs cid year ml_year, /// 
	cluster(cid) controls($econ $inst) ///
	autosample pre(3) ///
	tol(1) fe(cid year nyc)
estadd local cntrl_econ "\cmark"
estadd local cntrl_pol "\cmark"

// Output results for Table 3, Panel A (BJS) and Table A15
//	NYC dropped because fixed-effects are not reported by BJS package.

esttab seat_total_nc seat_total_econ seat_total_full seat_court seat_party ///
	using "output/tables/table-A15.tex", /// 
	 star(* .1 ** .05 *** .01) b(3) se(3) ///
	 order(tau trade_ln fdi_stock_ln gdp_ln ///
	  		gdppc_ln growth v2x_rule v2x_rule pre1 pre2 pre3) drop(nyc) ///
	 s(cntrl_econ cntrl_pol pre_p N, ///
	 	label("Economic Controls" "Political Controls" "Pre-trends p-value" "Observations")) label ///
	 replace
	 


/* 
	Panel B - Nationality of parties to ICC arbitrations
*/

/* PPML Results */

ppmlhdfe total i.ml, absorb(cid year) cluster(cid)
est store icc_party_base
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1993"

// Estimate pretrends p-value
ppmlhdfe total i.ml F(3/1)i.ml_treat, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_party_base
estadd local pretrents_p $temp_p
macro drop temp_p

ppmlhdfe total i.ml $econ, absorb(cid year) cluster(cid)
est store icc_party_cntrls1
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1993"

// Estimate pretrends p-value
ppmlhdfe total i.ml F(3/1)i.ml_treat $econ, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_party_cntrls1
estadd local pretrents_p $temp_p
macro drop temp_p

ppmlhdfe total i.ml $econ $inst, absorb(cid year) cluster(cid) d
est store icc_party_cntrls2
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1993"

// Estimate pretrends p-value
ppmlhdfe total i.ml F(3/1)i.ml_treat $econ $inst, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_party_cntrls2
estadd local pretrents_p $temp_p
macro drop temp_p

ppmlhdfe complainant i.ml $econ $inst, absorb(cid year) cluster(cid)
est store icc_complainant_cntrl2
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1994"

// Estimate pretrends p-value
ppmlhdfe complainant i.ml F(3/1)i.ml_treat $econ $inst, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_complainant_cntrl2
estadd local pretrents_p $temp_p
macro drop temp_p

ppmlhdfe defendant i.ml $econ $inst, absorb(cid year) cluster(cid)
est store icc_defendant_cntrl2
estadd local fe_year "\cmark"
estadd local fe_unit "\cmark"
estadd local start_year "1994"

// Estimate pretrends p-value
ppmlhdfe defendant i.ml F(3/1)i.ml_treat $econ $inst, absorb(cid year) cluster(cid)
test 1F3.ml_treat 1F2.ml_treat 1F.ml_treat
glo temp_p = round(r(p), .001)
est restore icc_defendant_cntrl2
estadd local pretrents_p $temp_p
macro drop temp_p

// Output results for Table 3, Panel B (PPML) and Table A16

esttab  icc_party_base icc_party_cntrls1 icc_party_cntrls2 icc_complainant_cntrl2 icc_defendant_cntrl2 ///
	using "output/tables/table-A16.tex", /// 
	label s(start_year fe_year fe_unit pretrents_p N, ///
	label("Start Year" "Year FE?" "Country FE?" "Pretrends p-value" "Observations") fmt(%9.0gc)) /// 
	star(* .1 ** .05 *** .01) b(3) se(3) booktabs compress ///
	mgroups("Total" "Complain." "Defendant", pattern(1 0 0 1 1) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) ///
			span erepeat(\cmidrule(lr){@span})) ///
	drop(0.* _cons) noomitted  ///
	nomtitles nogap replace
	
/* BJS Analyses */

qui eststo nat_total_nc: did_imputation total_ihs cid year ml_year, /// 
	cluster(cid)  ///
	autosample pre(3) ///
	tol(1) fe(cid year)
estadd local cntrl_econ ""
estadd local cntrl_pol ""

qui eststo nat_total_econ: did_imputation total_ihs cid year ml_year, /// 
	cluster(cid) controls($econ) ///
	autosample pre(3) ///
	tol(1) fe(cid year)
estadd local cntrl_econ "\cmark"
estadd local cntrl_pol ""

qui eststo nat_total_full: did_imputation total_ihs cid year ml_year, /// 
	cluster(cid) controls($econ $inst) ///
	autosample pre(3) ///
	tol(1) fe(cid year nyc)
estadd local cntrl_econ "\cmark"
estadd local cntrl_pol "\cmark"

qui eststo nat_comp: did_imputation complainant_ihs cid year ml_year, /// 
	cluster(cid) controls($econ $inst) ///
	autosample pre(3) ///
	tol(1) fe(cid year nyc)
estadd local cntrl_econ "\cmark"
estadd local cntrl_pol "\cmark"
	
qui eststo nat_def: did_imputation defendant_ihs cid year ml_year, /// 
	cluster(cid) controls($econ $inst) ///
	autosample pre(3) ///
	tol(1) fe(cid year nyc)
estadd local cntrl_econ "\cmark"
estadd local cntrl_pol "\cmark"
	

// Output results for Table 3, Panel B (BJS) and Table A17
//	NYC dropped because fixed-effects are not reported by BJS package.

esttab nat_total_nc nat_total_econ nat_total_full nat_comp nat_def ///
	using "output/tables/table-A17.tex", /// 
	star(* .1 ** .05 *** .01) b(3) se(3) ///
	order(tau trade_ln fdi_stock_ln gdp_ln ///
	  		gdppc_ln growth v2x_rule v2x_rule pre1 pre2 pre3) drop(nyc) ///
	s(cntrl_econ cntrl_pol pre_p N, ///
		label("Economic Controls" "Political Controls" "Pre-trends p-value" "Observations")) label ///
	 replace


/* Figure A6 */

// Panel A - Seat of Arbitration

qui ppmlhdfe total_seat F(3/1)i.ml_treat i.ml $econ $inst, absorb(cid year) cluster(cid)

coefplot, nolabel drop(0*) keep(*ml_treat 1.ml) omitted vertical ci(95) yline(0) ///
	xlabel(, labsize(large)) ytitle(, size(large)) ///
	ylabel(, labsize(large)) ytitle(, size(large)) ///
	scale(1.5) mcolor(black) ///
	ciopts(lwidth(.75) lcolor(black)) ///
	yline(0, lcolor(red)) ///
	msize(2) msymbol(dot) ///
	rename(1F3.ml_treat = "t-3" ///
		   1F2.ml_treat = "t-2" ///
		   1F.ml_treat = "t-1" ///
		   1.ml = "t + ...")
graph export "output/figures/figure-A7-a.pdf", replace

// Panel B - Nationality of Party
qui ppmlhdfe total F(3/1)i.ml_treat i.ml $econ $inst, absorb(cid year) cluster(cid)

coefplot, nolabel drop(0*) keep(*ml_treat 1.ml) omitted vertical ci(95) yline(0) ///
	xlabel(, labsize(large)) ytitle(, size(large)) ///
	ylabel(, labsize(large)) ytitle(, size(large)) ///
	scale(1.5) mcolor(black) ///
	ciopts(lwidth(.75) lcolor(black)) ///
	yline(0, lcolor(red)) ///
	msize(2) msymbol(dot) ///
	rename(1F3.ml_treat = "t-3" ///
		   1F2.ml_treat = "t-2" ///
		   1F.ml_treat = "t-1" ///
		   1.ml = "t + ...")
graph export "output/figures/figure-A7-b.pdf", replace

